import enum


class TaskStatus(enum.IntEnum):
    """
    有效状态机：管理是哪个字幕
    """
    start_dialog = 0
    do = 1
    win_dialog = 2
    fail_dialog = 3
    win_over = 4
    fail_over = 5
    win_dialog_temp = 6
    fail_dialog_temp = 7
    win_over_temp = 8
    fail_over_temp = 9
    fail_found_dialog = 10


class BaseTask:
    def __init__(self, begin_dialog, win_dialog, fail_dialog, fail_found_dialog):
        self.begin_dialog = begin_dialog
        self.win_dialog = win_dialog
        self.fail_dialog = fail_dialog
        self.fail_found_dialog = fail_found_dialog
        self.task_status = TaskStatus.start_dialog

    def do_task(self, key_down, key_list):
        """
        让字幕向下滚动
        :param key_down:
        :param key_list:
        :return:
        """
        if key_down:
            # 滚动开始字幕
            if self.task_status == TaskStatus.start_dialog:
                self.begin_dialog.next_text()
                if self.begin_dialog.is_finish():
                    self.task_status = TaskStatus.do
            # 滚动胜利字幕
            elif self.task_status == TaskStatus.win_dialog:
                self.win_dialog.next_text()
                if self.win_dialog.is_finish():
                    self.task_status = TaskStatus.win_over
            # 滚动失败字幕
            elif self.task_status == TaskStatus.fail_dialog:
                self.fail_dialog.next_text()
                if self.fail_dialog.is_finish():
                    self.task_status = TaskStatus.fail_over
            elif self.task_status == TaskStatus.fail_found_dialog:
                self.fail_found_dialog.next_text()
                if self.fail_found_dialog.is_finish():
                    self.task_status = TaskStatus.fail_over
        # 任务进行中
        if self.task_status == TaskStatus.do:
            self.do_self_task(key_down, key_list)

    def draw(self, current_surface, x, y):
        """
        判断当前状态并显示何种字幕
        :param current_surface:
        :param x:
        :param y:
        :return:
        """
        # 任务状态为结束
        if self.task_status == TaskStatus.win_over or \
                self.task_status == TaskStatus.fail_over:
            return
        # 开始的对话框
        elif self.task_status == TaskStatus.start_dialog:
            self.begin_dialog.draw(current_surface)
        # 胜利的对话框
        elif self.task_status == TaskStatus.win_dialog:
            self.win_dialog.draw(current_surface)
        # 失败的对话框
        elif self.task_status == TaskStatus.fail_dialog:
            self.fail_dialog.draw(current_surface)
        # 被发现了—失败的对话框
        elif self.task_status == TaskStatus.fail_found_dialog:
            self.fail_found_dialog.draw(current_surface)

        self.draw_task(current_surface, x, y)

    def do_self_task(self, key_down, key_list):
        """
        每个类进行重写 做自己的任务
        :param key_down:
        :param key_list:
        :return:
        """
        pass

    def get_state(self):
        return self.task_status

    def draw_task(self, current_surface, x, y):
        """
        每个类进行重写 显示自己的字幕
        :return:
        """
        pass
